//
//  DeviceDetialTableViewController.m
//  AlinkDemo
//
//  Created by Vincent on 2017/10/28.
//  Copyright © 2017年 aliyun. All rights reserved.
//

#import "DeviceDetialTableViewController.h"
#import "DeviceRenameViewController.h"
#import "PopViewController.h"
#import "CallViewController.h"

@interface DeviceDetialTableViewController ()
@property (weak, nonatomic) IBOutlet UIButton *deviceImageButton;
@property (weak, nonatomic) IBOutlet UILabel *batteryLabel;
@property (weak, nonatomic) IBOutlet UILabel *wifiLabel;
@property (weak, nonatomic) IBOutlet UISwitch *alarmSW;
@property (weak, nonatomic) IBOutlet UISwitch *quiteSW;

- (IBAction)showCallAction:(id)sender;
@end

@implementation DeviceDetialTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);

    [SVProgressHUD show];
    [YKBusinessFramework equesGetDeviceInfoWithUid:self.deviceDict[@"uid"]];
    
    [self.alarmSW addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
    //省电模式btn打开门铃灯
    [self.quiteSW addTarget:self action:@selector(switchChangeQuite:) forControlEvents:UIControlEventValueChanged];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//省电模式btn打开门铃灯
-(void)switchChangeQuite:(UISwitch *)sw{
    [YKBusinessFramework equesSetDoorbellLightWtihUid:self.deviceDict[@"uid"] status:sw.on];
}

-(void)switchChange:(UISwitch *)sw{
    [YKBusinessFramework equesSetPirEnableWithUid:self.deviceDict[@"uid"] status:sw.on];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMessageResult:) name:equesOnMessageResultNotification object:nil];
    self.tabBarController.navigationItem.rightBarButtonItem = nil;

}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark 收到的消息
- (void)onMessageResult:(NSNotification *)notification{

    [SVProgressHUD dismiss];
    
    NSDictionary *messageResult = [notification object];

    NSString *method = messageResult[@"method"];
    if ([method isEqualToString:@"deviceinfo_result"]) {
        self.batteryLabel.text = [NSString stringWithFormat:@"当前电量:%@",messageResult[@"battery_level"]];
        self.wifiLabel.text = [NSString stringWithFormat:@"当前网络:%@",messageResult[@"wifi_config"]];
        self.alarmSW.on = [messageResult[@"alarm_enable"] integerValue]==1?YES:NO;
        //门铃灯开关
        self.quiteSW.on = [messageResult[@"db_light_enable"] integerValue]==1?YES:NO;

    }else if ([method isEqualToString:@"rmbdy_result"]){
        NSInteger code = [messageResult[@"code"] integerValue];
        if (code==4000) {
            [SVProgressHUD showSuccessWithStatus:@"删除成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showSuccessWithStatus:@"删除失败"];

        }
    }
    
}




#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==1 && indexPath.row==0) {
        DeviceRenameViewController *vc= [[DeviceRenameViewController alloc] initWithNibName:@"DeviceRenameViewController" bundle:nil];
        vc.bid = self.deviceDict[@"bid"];
        MZFormSheetPresentationViewController *sheet = [[MZFormSheetPresentationViewController alloc] initWithContentViewController:vc];
        sheet.presentationController.contentViewSize = CGSizeMake(280, 250);
        sheet.presentationController.shouldDismissOnBackgroundViewTap = NO;
        sheet.contentViewCornerRadius=5.f;
        sheet.contentViewControllerTransitionStyle=MZFormSheetPresentationTransitionStyleDropDown;
        sheet.presentationController.shouldCenterVertically= YES;
        sheet.presentationController.shouldCenterHorizontally= YES;
        sheet.presentationController.movementActionWhenKeyboardAppears = MZFormSheetActionWhenKeyboardAppearsCenterVertically;

        sheet.willDismissContentViewControllerHandler=^(UIViewController *v){
    
        };
        
        [self presentViewController:sheet animated:YES completion:nil];
    }else if (indexPath.section==1 && indexPath.row==1) {
        PopViewController *vc= [[PopViewController alloc] initWithNibName:@"PopViewController" bundle:nil];
        NSString *str = @"您确定要删除设备吗?";
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attStr addAttribute:NSForegroundColorAttributeName
                           value:[UIColor redColor]
                           range:[str rangeOfString:@"删除"]];
        vc.attString =attStr;

        __weak typeof(self) weakSelf = self;

        vc.conformBlock = ^{
            [YKBusinessFramework equesDelDeviceWithBid:weakSelf.deviceDict[@"bid"]];
        };

        MZFormSheetPresentationViewController *sheet = [[MZFormSheetPresentationViewController alloc] initWithContentViewController:vc];
        sheet.presentationController.contentViewSize = CGSizeMake(280, 160);
        sheet.presentationController.shouldDismissOnBackgroundViewTap = NO;
        sheet.contentViewCornerRadius=5.f;
        sheet.presentationController.movementActionWhenKeyboardAppears = MZFormSheetActionWhenKeyboardAppearsCenterVertically;

        sheet.contentViewControllerTransitionStyle=MZFormSheetPresentationTransitionStyleDropDown;
        sheet.presentationController.shouldCenterVertically= YES;
        sheet.presentationController.shouldCenterHorizontally= YES;
        sheet.willDismissContentViewControllerHandler=^(UIViewController *v){

        };

        [self presentViewController:sheet animated:YES completion:nil];
    }
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"Call"]) {

        CallViewController *vc = [segue destinationViewController];
        vc.bid = self.deviceDict[@"bid"];
        vc.title = self.deviceDict[@"name"];
    }
}


- (IBAction)showCallAction:(id)sender {
}
@end
